#ifndef __HTTPSERVER_H__
#define __HTTPSERVER_H__

#include "Spark/Net/TcpServer.h"

class HttpRequest;
class HttpResponse;

class HttpServer : NonCopyable
{
  public:
    using HttpCallback = std::function<void(const HttpRequest&, HttpResponse&)>;

    HttpServer(EventLoop* loop,
               const SocketAddr& listenAddr,
               const string& name);

    EventLoop* getLoop() const
    {
        return server_.getLoop();
    }

    // Not thread safe, callback be registered before calling start().
    void setHttpCallback(const HttpCallback& cb)
    {
        httpCallback_ = cb;
    }

    void setThreadNum(int numThreads)
    {
        server_.setIoLoopNum(numThreads);
    }

    void start();

  private:
    void onConnection(const TcpConnectionPtr& conn);
    void onMessage(const TcpConnectionPtr& conn,
                   MsgBuffer* buf,
                   TimePoint receiveTime);
    void onRequest(const TcpConnectionPtr&, const HttpRequest&);

    TcpServer server_;
    HttpCallback httpCallback_;
};
#endif  // __HTTPSERVER_H__